﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace common
{
    public class QuestionManagement
    {

        public string Question { get; set; }
        public string[] Answers { get; set; }
        public int Correct { get; set; } //start at 0

        public QuestionManagement()
        {
            
        }

        public QuestionManagement(string question, string[] answers, int correct)
        {
            this.Question = question;
            this.Answers = new string[answers.Count()];
            for (var i = 0; i < answers.Count(); i++)
            {
                this.Answers[i] = answers[i];
            }
            this.Correct = correct;
        }

        public QuestionManagement(string question, List<string> answers, int correct)
        {
            this.Question = question;
            this.Answers = new string[answers.Count()];
            for (var i = 0; i < answers.Count(); i++)
            {
                this.Answers[i] = answers[i];
            }
            this.Correct = correct;
        }

        public string ToString(int index)
        {
            for (int i = 0; i < this.Answers.Count(); i++)
            {
                this.Answers[i] = Optimized(this.Answers[i]);
            }
            Question = Optimized(this.Question);
            string str = "";
            str = string.Format("::{0}::{1}", index.ToString(), this.Question);
            str += "{";
            for (var i = 0; i < this.Answers.Count(); i++)
            {
                if (i == this.Correct)
                {
                    str += "=";
                }
                else
                {
                    str += "~";
                }
                str += string.Format("{0}", Optimized(this.Answers[i].Trim()).ToString());
                if (i < this.Answers.Count() - 1)
                {
                    str += " ";
                }
            }
            str += "}";
            return str;
        }

        public String Optimized(String oldStr)
        {
            if (oldStr.Length < 0)
            {
                return string.Empty;
            }
            var str = string.Empty;

            //Thay thế newline bằng </br>
            //oldStr = oldStr.replaceAll("\\r|\\n", "</br>");
            oldStr = oldStr.Replace("\\r", "");
            oldStr = oldStr.Replace("\n", "</br>");
            oldStr = oldStr.Replace("<font face\\=\"times new roman, times, serif\">π</font>", "π");

            //set font cho π
            for (int i = 0; i < oldStr.Length; i++)
            {
                if (oldStr[i] == 'π')
                {
                    str += "<font face\\=\"times new roman, times, serif\">π</font>";
                }
                else
                {
                    str += oldStr[i];
                }
            }
            oldStr = str;
            str = "";

            //Thêm ký tự \ trước mỗi dấu = va ~
            try
            {
                if (oldStr[0] == '=' || oldStr[0] == '~')
                {
                    str += "\\" + oldStr[0];
                }
                else
                {
                    str += oldStr[0];
                }
                for (int i = 1; i < oldStr.Length; i++)
                {
                    if ((oldStr[i] == '=' || oldStr[i] == '~') && oldStr[i - 1] == '\\')
                    {
                        str += oldStr[i];
                    }
                    else if ((oldStr[i] == '=' || oldStr[i] == '~') && oldStr[i - 1] != '\\')
                    {
                        str += "\\" + oldStr[i];
                    }
                    else
                    {
                        str += oldStr[i];
                    }
                }
            }
            catch (Exception e)
            {
                return e.ToString();
            }
            return str;
        }

        public override string ToString()
        {
            return string.Format("{0}\r\nA. {1}\r\nB. {2}\r\nC. {3}\r\nD. {4}", Question, Answers[0], Answers[1], Answers[2], Answers[3]);
        }

        public string EncodeQuestion()
        {
            for (var i = 0; i < this.Answers.Count(); i++)
            {
                this.Answers[i] = Optimized(this.Answers[i]);
            }
            Question = Optimized(this.Question);
            string str = "";
            str = string.Format("{0}",  this.Question);
            str += "{";
            for (var i = 0; i < this.Answers.Count(); i++)
            {
                if (i == this.Correct)
                {
                    str += "=";
                }
                else
                {
                    str += "~";
                }
                str += string.Format("{0}", Optimized(this.Answers[i].Trim()));
                if (i < this.Answers.Count() - 1)
                {
                    str += " ";
                }
            }
            str += "}";
            return str;
        }

        public QuestionManagement DecodeQuestion(string question)
        {
            return FileUtils.ParseQuestion(question);
        }
    }
}
